#include <sys/io.h> 
#include <unistd.h> 
#include <stdio.h> 
#include <stdlib.h>
#include <string.h>


#include "ec_read_write.h"



int rtc_read_byte(int nbaseaddr, int *data)
{
	if (iopl(3) < 0) 
	{
		#if 0
		wattron(stdscr, color);
		mvprintw(LINES - 2, 0, "iopl access error! run as root, sudo ./target/bin/test_tool_BM6J58 \n"); 
		mvprintw(LINES - 3, 0, "You have chosen %d ", 	Index);
		wattroff(stdscr, color);
		#endif
		printf( "iopl(3) err ");


		return -1;
		//exit(1); 
	}

	for(int Index=0;Index<0x0E;Index++)
	{

	
	outb(Index,nbaseaddr);
	*data=inb(nbaseaddr+1);
	//printf("%02X ", data);


	}
	//printf("    fggg22\n");
	return 0;


}


int pnp_read_byte(int Index, int* data)
{

	//mvprintw(LINES - 4, 0, "You have chosen addr %08Xh ", 	Index);


	if (iopl(3) < 0) 
	{
		#if 0
		wattron(stdscr, color);
		mvprintw(LINES - 2, 0, "iopl access error! run as root, sudo ./target/bin/test_tool_BM6J58 \n"); 
		mvprintw(LINES - 3, 0, "You have chosen %d ", 	Index);
		wattroff(stdscr, color);
		#endif
		printf( "iopl(3) err ");


		return -1;
		//exit(1); 
	}

	outb(0x07,0x2E);
	outb(Index >> 8,0x2F);

	
	outb(Index&0xFF,0x2E);
	*data=inb(0x2F);
	return 0;

}

int pnp_write_byte(int Index,int data)
{

	//mvprintw(LINES - 4, 0, "You have chosen addr %08Xh ", 	Index);


	if (iopl(3) < 0) 
	{
		printf( "iopl(3) err ");



		return -1;
		//exit(1); 
	}

	outb(0x07,0x2E);
	outb(Index >> 8,0x2F);

	
	outb(Index&0xFF,0x2E);

	outb(data,0x2F);
	return 0x00;

}

int pnp_read_256bytes(int Index,int nlen,unsigned char *bufdata)
{

	//mvprintw(LINES - 4, 0, "You have chosen addr %08Xh ", 	nbase_addrpara);


	if (iopl(3) < 0) 
	{
		return -1;
		//exit(1); 
	}

	outb(0x07,0x2E);
	outb(Index,0x2F);

	
	for(int i=0;i<256;i++)
	{
		outb(i,0x2E);
		bufdata[i]=inb(0x2F);

#if 0
		char str[25];
		//itoa(i, str, 16);
		sprintf(str,"%02X ", data );
		//sprintf(str,"%02X ",nVal);
		//mvprintw(LINES - 8+ i/32, i%32*3, "%02X ", 	data);
		
		memcpy(p_type_cell_data->data_cell[i/const_nMaxCell][i%const_nMaxCell],str,2);
#endif	
	}
	return 0;
}


int ecio_read_byte(unsigned short Index)
{
		if (iopl(3) < 0) 
	{
		#if 0
		wattron(stdscr, color);
		mvprintw(LINES - 2, 0, "iopl access error! run as root, sudo ./target/bin/test_tool_BM6J58 \n"); 
		mvprintw(LINES - 3, 0, "You have chosen %d ", 	addr);
		wattroff(stdscr, color);
		#endif
		printf( "iopl(3) err ");

		return -1;
		//exit(1); 
	}

  int ret = 0xff; 

  outb(0x2e,0x2e);
  outb(0x11,0x2f);
  outb(0x2f,0x2e);
  outb(Index >> 8,0x2f);

  outb(0x2e,0x2e);
  outb(0x10,0x2f);
  outb(0x2f,0x2e);
  outb(Index&0xFF,0x2f);

  outb(0x2e,0x2e);
  outb(0x12,0x2f);
  outb(0x2f,0x2e);
  ret = inb(0x2f);

  //printf("ret is 0x%x!\n", ret);
  return ret;
}


int ecio_write_byte(unsigned short Index,int data)
{

	if (iopl(3) < 0) 
	{
		#if 0
		wattron(stdscr, color);
		mvprintw(LINES - 2, 0, "iopl access error! run as root, sudo ./target/bin/test_tool_BM6J58 \n"); 
		mvprintw(LINES - 3, 0, "You have chosen %d ", 	addr);
		wattroff(stdscr, color);
		#endif
		printf( "iopl(3) err ");

		return -1;
		//exit(1); 
	}

  int ret = 0xff; 

  outb(0x2e,0x2e);
  outb(0x11,0x2f);
  outb(0x2f,0x2e);
  outb((Index >> 8)&0xFF,0x2f);

  outb(0x2e,0x2e);
  outb(0x10,0x2f);
  outb(0x2f,0x2e);
  outb(Index&0xFF,0x2f);

  outb(0x2e,0x2e);
  outb(0x12,0x2f);
  outb(0x2f,0x2e);
  outb(data,0x2f);

//  printk("ret is 0x%x!\n", ret);
  return ret;
}

int ecio_read_256bytes(unsigned short IndexPara,int nlen,unsigned char *bufdata)
{

	//mvprintw(LINES - 4, 0, "You have chosen addr %08Xh ", 	nbase_addrpara);
	//printf( "You have chosen addr %d ", 	nbase_addr);

	if (iopl(3) < 0) 
	{
		//printf( "iopl(3) err ");
		return -1;
		//exit(1); 
	}


	//unsigned char ecio_read(unsigned char index);
	unsigned short nbaseaddr=(unsigned short)IndexPara;



	for(int i=0; i<256;i++)
	{
		bufdata[i]=ecio_read_byte(nbaseaddr+i);;

		#if 0
		//if(index%16==0)
		//	printf("\n");
		unsigned char data=ecio_read_byte(nbaseaddr+index);


			char str[25];
		//itoa(i, str, 16);
		sprintf(str,"%02X ", data );
		//sprintf(str,"%02X ",nVal);
		//mvprintw(LINES - 8+ i/32, i%32*3, "%02X ", 	data);
		
		memcpy(p_type_cell_data->data_cell[index/const_nMaxCell][index%const_nMaxCell],str,2);
			
		//printf("%02X ", nval);	
		#endif


	}
	//printf("\n");


	return 0;
}